<template>
  <Card style="width:850px;margin: auto;">
    <Form :model="formLeft" label-position="left" :rules="validateLibForm" :label-width="100" style="width: 300px;margin: auto;">


      <FormItem label="设备代码:" prop="code">
        <Input v-model="formLeft.code" :maxlength="10"></Input>
      </FormItem>

      <FormItem label="设备类别:" prop="category">
        <Input v-model="formLeft.category" :maxlength="10"></Input>
      </FormItem>

      <FormItem label="描述:" prop="description">
        <Input v-model="formLeft.description" :maxlength="100" type="textarea" style="width: 200px"/>
      </FormItem>


      <FormItem label="购买时间" prop="purchaseDate" style="width: 500px">
        <DatePicker type="date"
                    :value="formLeft.purchaseDate"
                    @on-change="datePickerSelect($event,'purchaseDate')"
                    style="width: 200px">

        </DatePicker>
      </FormItem>

      <FormItem label="工艺代码" prop="processCode">
        <Select v-model="formLeft.processCode">
          <Option v-for="item in processList" :value="item.code">{{ item.code }}</Option>
        </Select>
      </FormItem>

      <FormItem label="使用状态" prop="status">
        <Select v-model="formLeft.status">
          <Option v-for="item in statusList" :value="item.value" :key="item.value">{{ item.label }}</Option>
        </Select>
      </FormItem>

      <row type="flex" justify="center">
        <i-col span="20">

        </i-col>
        <i-col span="30">
          <Button type="primary" @click="DeviceUpdateById">修改</Button>
          <Button type="success" style="margin-left: 160px" @click="back">返回</Button>
        </i-col>
      </row>

    </Form>
  </Card>
</template>

<script>
  import {DeviceFindByCode,DeviceUpdateById,ProcessFindAll} from "../../../api/data"

  export default {
    data() {
      return {
        page: 1,//page 第几页
        pageSize: 10,//pageSize:每页几条数据
        total: 0,
        statusList: [
          {
            value: '0',
            label: '使用'
          }, {
            value: '1',
            label: '不使用'
          }
        ],
        processList:[],
        formLeft: {
          code: '',
          category: '',
          status: '',
          description: '',
          purchaseDate: '',
          processCode: '',
        },
        validateLibForm: {
          code: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          category: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          description: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          purchaseDate: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          processCode: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
          status: [
            {required: true, message: '请输入', trigger: 'blur'}
          ],
        }
      }
    },
    mounted() {
      const codeFlag = this.$route.query.code;
      if(codeFlag){
        this.formLeft.code = codeFlag;
        this.DeviceFindByCode();
      }
      this.ProcessFindAll();
    },
    methods: {
      datePickerSelect(value, key) {
        this.formLeft[key] = value
      },
      DeviceUpdateById() {
        if (this.formLeft.code !== null && this.formLeft.code !== "") {
          if (this.formLeft.category !== null && this.formLeft.category !== "") {
            if (this.formLeft.description !== null && this.formLeft.description !== "") {
              if (this.formLeft.purchaseDate !== null && this.formLeft.purchaseDate !== "") {
                if (this.formLeft.processCode !== null && this.formLeft.processCode !== "") {
                  if (this.formLeft.status !== null && this.formLeft.status !== "") {
                    DeviceUpdateById(this.formLeft).then(res => {
                      this.$Message.success("编辑成功");
                      this.$router.go(-1);
                    })
                  } else {
                    this.$Message.error("使用状态不能为空");
                  }
                } else {
                  this.$Message.error("工艺代码不能为空");
                }
              } else {
                this.$Message.error("购买时间不能为空");
              }
            } else {
              this.$Message.error("描述不能为空");
            }
          } else {
            this.$Message.error("设备类别不能为空");
          }
        } else {
          this.$Message.error("设备代码不能为空");
        }
      },
      ProcessFindAll() {
        var data = {
          page: this.page,
          pageSize: this.pageSize,
        };
        ProcessFindAll(data).then(data => {
          if (data.data.code == 0) {
            this.processList = data.data.data.list;
            //   console.log(this.processList)
          }
        })
      },
      back(){
        this.$router.go(-1);
      },
      DeviceFindByCode(){
        var data =  {
          code:this.formLeft.code,
        }
        DeviceFindByCode(data).then(res => {
          //console.log( res.data.data)
          this.formLeft = res.data.data;
        })
      }
    },

  }
</script>


